package it.lefo.dateReminder;

import it.lefo.dateReminder.bo.ContactsRepository;
import it.lefo.dateReminder.helper.DbHelper;
import it.lefo.dateReminder.helper.Utilities;
import it.lefo.dateReminder.model.Contact;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.exina.android.calendar.CalendarActivity;
import com.exina.android.calendar.TestActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;


public class DateReminder extends Activity{
	
	private List<Contact> _contacts = new ArrayList<Contact>();
	private ContactsRepository _cntRepo;
	private SQLiteDatabase _db;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //DB connection
        DbHelper dbh = new DbHelper(this);          
		try {
			//db connection
			_db = dbh.getOrCreateDataBase();
			_cntRepo = new ContactsRepository(_db);
		}catch(Exception e){
			Log.e("DateReminder", "Errore durante la connessione al db. Exc Message: " + e.getMessage() + ". Exc StakTrace: " +  e.getStackTrace());
		}
        
// >>>> Integrazione Calendar Widget        
//      addPreferencesFromResource(R.xml.test);
//        
//      findPreference("show").setIntent(new Intent(Intent.ACTION_VIEW).setDataAndType(null, CalendarActivity.MIME_TYPE));
//		findPreference("pick").setOnPreferenceClickListener(this);
//		findPreference("about").setOnPreferenceClickListener(this);
//		startActivity(new Intent(Intent.ACTION_PICK).setDataAndType(null, CalendarActivity.MIME_TYPE));
// <<<< Integrazione Calendar Widget
        
		try {	    				
	        DatePicker datepicker = (DatePicker)findViewById(R.id.dpkDate);
	        Button btnAddEvent = (Button)findViewById(R.id.btnAddEvent);
	        Button btnGetEventList = (Button)findViewById(R.id.btnGetEvents);
	        Button btnGetAllEventList = (Button)findViewById(R.id.btnGetAllEvents);
	        Button btnGetCurrentEvents = (Button)findViewById(R.id.btnGetCurrentEvents);
	        
	        datepicker.init(1984,9, 03, null);
	        btnGetEventList.setOnClickListener(new View.OnClickListener() {			
				@Override
				public void onClick(View v) {
					DatePicker datepicker = (DatePicker)findViewById(R.id.dpkDate);
					Date dtSelected = new Date(datepicker.getYear()-1900, datepicker.getMonth(), datepicker.getDayOfMonth());
					
					//
			        _contacts = _cntRepo.getAllContactsInDate(dtSelected);
					
					//
					Bundle bundle = new Bundle();
					bundle.putString("dateSelected", Utilities.DateToString(dtSelected, "dd/MM/yyyy"));
					bundle.putSerializable("contacts", (Serializable) _contacts);
					Intent i = new Intent(DateReminder.this,EventsInDateView.class);
					i.putExtras(bundle);					
					startActivity(i);
				}
			});
	        
	        btnGetAllEventList.setOnClickListener(new View.OnClickListener() {			
				@Override
				public void onClick(View v) {
					//
					_contacts = _cntRepo.getAllContacts();
					
					//
					Bundle bundle = new Bundle();
					bundle.putSerializable("contacts", (Serializable) _contacts);
					Intent i = new Intent(DateReminder.this,EventsInDateView.class);
					i.putExtras(bundle);
					startActivity(i);
				}
			});
	        
	        btnGetCurrentEvents.setOnClickListener(new View.OnClickListener() {			
				@Override
				public void onClick(View v) {
					DatePicker datepicker = (DatePicker)findViewById(R.id.dpkDate);
					Date dtSelected = Calendar.getInstance().getTime();
					
					//
					_contacts = _cntRepo.getAllBirthday(dtSelected);
					
					//
					Bundle bundle = new Bundle();
					bundle.putString("dateSelected", Utilities.DateToString(dtSelected, "dd/MM/yyyy"));
					bundle.putSerializable("contacts", (Serializable) _contacts);
					Intent i = new Intent(DateReminder.this,EventsInDateView.class);
					i.putExtras(bundle);					
					startActivity(i);
				}
			});
	        
	        btnAddEvent.setOnClickListener(new View.OnClickListener() {				
				@Override
				public void onClick(View v) {
					DatePicker datepicker = (DatePicker)findViewById(R.id.dpkDate);
					Date dtSelected = new Date(datepicker.getYear()-1900, datepicker.getMonth(), datepicker.getDayOfMonth());
					Bundle bundle = new Bundle();
					bundle.putString("dateSelected", Utilities.DateToString(dtSelected, "dd/MM/yyyy"));
					Intent i = new Intent(DateReminder.this,AddEventView.class);			
					i.putExtras(bundle);		
					startActivity(i);
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
    
// >>>> Integrazione Calendar Widget  
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if(resultCode==RESULT_OK) {
//            int year = data.getIntExtra("year", 0);   // get number of year
//            int month = data.getIntExtra("month", 0); // get number of month 0..11
//            int day = data.getIntExtra("day", 0);     // get number of day 0..31
//
//            // format date and display on screen
//            final Calendar dat = Calendar.getInstance();
//            dat.set(Calendar.YEAR, year);
//            dat.set(Calendar.MONTH, month);
//            dat.set(Calendar.DAY_OF_MONTH, day);
//            
//            SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd");
//            Toast.makeText(DateReminder.this, format.format(dat.getTime()), Toast.LENGTH_LONG).show();
//                    
//        }
//    }
//    
//    public boolean onPreferenceClick(Preference  preference) {
//		String key = preference.getKey();
//		if(key.equals("pick")) {
//			startActivityForResult(new Intent(Intent.ACTION_PICK).setDataAndType(null, CalendarActivity.MIME_TYPE), 100);
//		} else if(key.equals("about")) {
//			new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.app_name).setMessage("http://code.google.com/p/android-calendar-view/\n\nBy Chris Gao<chris@exina.net>").create().show();
//		}
//		return true;
//	}
// <<<< Integrazione Calendar Widget
}